1
|
|
|
import execa from 'execa' |
2
|
|
|
import * as fs from 'fs' |
3
|
|
|
import {NORMAL_EXTENSION_TYPE} from './extensions' |
4
|
|
|
import Pecl from './pecl' |
5
|
|
|
|
6
|
|
|
abstract class PhpExtension { |
7
|
|
|
abstract extension: string |
8
|
|
|
abstract alias: string |
9
|
|
|
|
10
|
|
|
// Extension settings |
11
|
|
|
default: boolean = true |
12
|
|
|
extensionType: string = NORMAL_EXTENSION_TYPE |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Check if the extension is enabled. |
16
|
|
|
*/ |
17
|
|
|
isEnabled = async (): Promise<boolean> => { |
18
|
|
|
const {stdout} = await execa('php', ['-m', '|', 'grep', this.extension]) |
19
|
|
|
const extensions = stdout.split('\n') |
20
|
|
|
|
21
|
|
|
return extensions.includes(this.extension) |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Check if the extension is installed. |
26
|
|
|
*/ |
27
|
|
|
isInstalled = async (): Promise<boolean> => { |
28
|
|
|
const {stdout} = await execa('pecl', ['list', '|', 'grep', this.extension]) |
29
|
|
|
return stdout.includes(this.extension) |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Install the extension. |
34
|
|
|
*/ |
35
|
|
|
install = async (): Promise<void> => { |
36
|
|
|
if (await this.isInstalled()) { |
37
|
|
|
console.log(`Extension ${this.extension} is already installed.`) |
38
|
|
|
return |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
const {stdout} = await execa('pecl', ['install', this.extension]) |
42
|
|
|
|
43
|
|
|
const installRegex = new RegExp(`Installing '(.*${this.alias}.so)'`, 'g').test(stdout) |
44
|
|
|
if (!installRegex) |
45
|
|
|
throw new Error(`Unable to find installation path for ${this.extension}. Result:\n\n`) |
46
|
|
|
|
47
|
|
|
if (stdout.includes('Error:')) |
48
|
|
|
throw new Error(`Found installation path, but installation still failed: \n\n${stdout}`) |
49
|
|
|
|
50
|
|
|
const phpIniPath = await Pecl.getPhpIni() |
51
|
|
|
let phpIni = await fs.readFileSync(phpIniPath, 'utf-8') |
52
|
|
|
|
53
|
|
|
// TODO: Fix duplicate extension entires in php.ini |
54
|
|
|
const extensionRegex = new RegExp(`(zend_extension|extension)="(.*${this.alias}.so)"`, 'g').test(phpIni) |
55
|
|
|
if (!extensionRegex) |
56
|
|
|
throw new Error(`Unable to find definition in ${phpIniPath} for ${this.extension}`) |
57
|
|
|
|
58
|
|
|
console.log(`Extension ${this.extension} has been installed.`) |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Uninstall the extension. |
63
|
|
|
*/ |
64
|
|
|
uninstall = async (): Promise<void> => { |
65
|
|
|
await execa('pecl', ['uninstall', this.extension]) |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Enable the extension. |
70
|
|
|
*/ |
71
|
|
|
enable = async (): Promise<void> => { |
72
|
|
|
if (await this.isEnabled()) { |
73
|
|
|
console.log(`Extension ${this.extension} is already enabled.`) |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
const phpIniPath = await Pecl.getPhpIni() |
77
|
|
|
let phpIni = await fs.readFileSync(phpIniPath, 'utf-8') |
78
|
|
|
const regex = new RegExp(`(zend_extension|extension)="(.*${this.alias}.so)"\/n`, 'g') |
79
|
|
|
phpIni = phpIni.replace(regex, '') |
80
|
|
|
phpIni = `${this.extensionType}="${this.alias}.so"\n${phpIni}` |
81
|
|
|
|
82
|
|
|
await fs.writeFileSync(phpIniPath, phpIni) |
83
|
|
|
|
84
|
|
|
console.log(`Extension ${this.extension} has been enabled`) |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Disable the extension. |
89
|
|
|
*/ |
90
|
|
|
disable = async (): Promise<void> => { |
91
|
|
|
const phpIniPath = await Pecl.getPhpIni() |
92
|
|
|
let phpIni = await fs.readFileSync(phpIniPath, 'utf-8') |
93
|
|
|
|
94
|
|
|
const regex = new RegExp(`;?(zend_extension|extension)=".*${this.alias}.so"\n`, 'g') |
95
|
|
|
phpIni = phpIni.replace(regex, '') |
96
|
|
|
|
97
|
|
|
await fs.writeFileSync(phpIniPath, phpIni) |
98
|
|
|
|
99
|
|
|
console.log(`Extension ${this.extension} has been disabled`) |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
export default PhpExtension |